import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { Api } from '../../../providers/api-service';
import { UserInfo } from '../../../global/ConstUrl';
import { UserDetialChallengePage } from '../../user/user-detial-challenge/user-detial-challenge';

/**
 * 好友排名
 * Ionic pages and navigation.
 */
@Component({
  selector: 'page-my-ranking',
  templateUrl: 'my-ranking.html',
})
export class MyRankingPage {
  bor_red0 = true;
  bor_red2=false;
  bor_red1=false;
  list1 = new Array();
  list1_me = new Array();
  pageindex = 1;
  userId = UserInfo().UserID;


  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public api: Api
  ) {
    this.GetAllRanking(this.pageindex, 1);
  }

  ionViewDidLoad() {
    console.log('好友排名 MyRankingPage');
  }
  forbidFinite=false;
  type=1;
  /**
   * tab选择页
   * @param key 
   */
  qiehuan(key) {
    this.bor_red0 = false;
    this.bor_red1 = false;
    this.bor_red2 = false;
    this.type=key+1;
    for (let index = 0; index < 3; index++) {
      if (key == index) {
        switch (key) {
          case 0:
            this.bor_red0 = true;
            break;
          case 1:
            this.bor_red1 = true;
            break;
          case 2:
            this.bor_red2 = true;
            break;
        }
        this.GetAllRanking(this.pageindex, this.type);
      }
    }
    console.log(this.bor_red0+"\n"+this.bor_red1+"\n"+this.bor_red2+"\n=============="+key);
    
  }
	/**
   * 下拉刷新
   * @param refresher 
   */
  doRefresh(refresher) {
    this.GetAllRanking(this.pageindex, this.type);
    setTimeout(() => {
      refresher.complete();
    }, 2000);
  }
  GetAllRanking(pageindex, type) {
    this.list1_me=new Array();
    this.list1 = new Array();
    this.api.postBody("V1/GetAllRanking", {
      "Type": type,
      "PageIndex": pageindex,
      "PageSize": 100
    })
      .then(data => {

        console.log("获取好友2");
        console.log(data);
        if (data.Code == 0) {
          let oblist = data.Data.Items;
          if (oblist.length <100) {
            this.forbidFinite=true;
          } else {
            return false;
          }
          for (let index = 0; index < oblist.length; index++) {
            let element = oblist[index];
            if (element.ID == this.userId) {
              this.list1_me.push(element);
              }
          }
          this.list1 = oblist;
        }
      })
  }
  // userDetial_challenge
  openPerson(item){
    this.navCtrl.push(UserDetialChallengePage, { "userID": item.ID});
  }
}
